package com.qs.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qs.constant.GoodsConstant;
import com.qs.constant.StatusConstant;
import com.qs.exception.ServiceException;
import com.qs.mapper.CommentMapper;
import com.qs.mapper.GoodsMapper;
import com.qs.mapper.StoreMapper;
import com.qs.pojo.dto.GoodsDto;
import com.qs.pojo.entity.CollectGoods;
import com.qs.mapper.CollectGoodsMapper;
import com.qs.pojo.entity.Comment;
import com.qs.pojo.entity.Goods;
import com.qs.pojo.entity.Store;
import com.qs.response.ResponseEnum;
import com.qs.service.ICollectGoodsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.aspectj.weaver.ast.Var;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 收藏的商品表 服务实现类
 * </p>
 *
 * @author author
 * @since 2023-12-10
 */
@Service
@RequiredArgsConstructor
public class CollectGoodsServiceImpl extends ServiceImpl<CollectGoodsMapper, CollectGoods> implements ICollectGoodsService {

    private final GoodsMapper goodsMapper;

    private final StoreMapper storeMapper;

    private final CommentMapper commentMapper;

    @Override
    public void saveCollectGoods(CollectGoods collectGoods) {
        if (collectGoods != null){
            QueryWrapper<CollectGoods> wrapper = new QueryWrapper<>();
            wrapper.eq("user_id",collectGoods.getUserId());
            wrapper.eq("goods_id",collectGoods.getGoodsId());
            CollectGoods collectSuc = baseMapper.selectOne(wrapper);
            if(null != collectSuc){
                throw new ServiceException(ResponseEnum.COLLECT_SUCCESS);
            }
            baseMapper.insert(collectGoods);
        }
    }

    @Override
    public Map<String,Object> getCollectGoodsList(Integer pageNo,Integer pageSize,Integer userId) {
        if(null != userId){
            Page<CollectGoods> pageInfo = new Page<>(pageNo,pageSize);
            QueryWrapper<CollectGoods> wrapper = new QueryWrapper<>();
            wrapper.eq("user_id", userId);
            Page<CollectGoods> collectGoodsPage = baseMapper.selectPage(pageInfo, wrapper);
            if(collectGoodsPage != null){
                List<CollectGoods> collectGoodsList = collectGoodsPage.getRecords();
                List<GoodsDto> list = new ArrayList<GoodsDto>(collectGoodsList.size());
                if(!CollectionUtils.isEmpty(collectGoodsList)){
                    collectGoodsList.stream().forEach(item -> {
                        QueryWrapper<Goods> goodsQueryWrapper = new QueryWrapper<>();
                        goodsQueryWrapper.eq("id",item.getGoodsId());
                        goodsQueryWrapper.eq("status",StatusConstant.STATUS_GOODS);
                        Goods goods = goodsMapper.selectOne(goodsQueryWrapper);
                        Store store = storeMapper.selectById(goods.getStoreId());

                        QueryWrapper<CollectGoods> collectWrapper = new QueryWrapper<>();
                        collectWrapper.eq("goods_id",item.getGoodsId());
                        Long collectNum = baseMapper.selectCount(collectWrapper);

                        QueryWrapper<Comment> commentWrapper = new QueryWrapper<>();
                        commentWrapper.eq("goods_id",item.getGoodsId());
                        Long commentNum = commentMapper.selectCount(commentWrapper);

                        GoodsDto goodsDto = new GoodsDto();
                        BeanUtils.copyProperties(goods, goodsDto);
                        goodsDto.setHomeName(store.getHomeName());
                        goodsDto.setStoreImage(store.getImage());
                        goodsDto.setKey(item.getId());
                        goodsDto.setGoodsId(item.getGoodsId());
                        goodsDto.setNumber(0);
                        goodsDto.setCollectNum(collectNum);
                        goodsDto.setCommentNum(commentNum);
                        list.add(goodsDto);
                    });
                    Map<String,Object> map = new HashMap<String,Object>();
                    map.put("data",list);
                    map.put("total",collectGoodsPage.getTotal());
                    return map;
                }
                return null;
            }
            return null;
        }
        return null;
    }

    @Override
    public Map<String, Object> findPerimeterGoods(Integer pageNo, Integer pageSize) {
        Page<Goods> pageInfo = new Page<>(pageNo,pageSize);
        QueryWrapper<Goods> wrapper = new QueryWrapper<>();
        wrapper.eq("status",StatusConstant.STATUS_GOODS);
        Page<Goods> goodsPage = goodsMapper.selectPage(pageInfo, wrapper);
        if (goodsPage != null) {
            List<Goods> goodsList = goodsPage.getRecords();
            List<GoodsDto> list = new ArrayList<>();
            if(!CollectionUtils.isEmpty(goodsList)) {
                goodsList.stream().forEach(goods -> {
                    Store store = storeMapper.selectById(goods.getStoreId());
                    QueryWrapper<CollectGoods> collectWrapper = new QueryWrapper<>();
                    collectWrapper.eq("goods_id",goods.getId());
                    Long collectNum = baseMapper.selectCount(collectWrapper);

                    QueryWrapper<Comment> commentWrapper = new QueryWrapper<>();
                    commentWrapper.eq("goods_id",goods.getId());
                    Long commentNum = commentMapper.selectCount(commentWrapper);

                    GoodsDto goodsDto = new GoodsDto();
                    BeanUtils.copyProperties(goods, goodsDto);
                    goodsDto.setHomeName(store.getHomeName());
                    goodsDto.setStoreImage(store.getImage());
                    goodsDto.setKey(goods.getId());
                    goodsDto.setGoodsId(goods.getId());
                    goodsDto.setNumber(0);
                    goodsDto.setCollectNum(collectNum);
                    goodsDto.setCommentNum(commentNum);
                    list.add(goodsDto);
                });
            }
            Map<String,Object> map = new HashMap<String,Object>();
            map.put("data",list);
            map.put("total",goodsPage.getTotal());
            return map;
        }
        return null;
    }
}
